<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Api\Controllers\Commons;

use App\Api\BaseApi;
use App\Models\BeanPackages;
use App\Models\Product;
use App\Models\ProductCategory;
use App\Models\User;
use App\Service\Product\ProductService;
use Illuminate\Database\Query\Builder;

class ProductApi extends BaseApi
{
    /**
     * 热门商品
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Pagination\Paginator|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|void
     */
    public function hotProducts()
    {
        return responses(ProductService::make()->baseList(null, 'is_hot'));
    }

    /**
     * 最新商品
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Pagination\Paginator|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|void
     */
    public function theLatestProducts()
    {
        return responses(ProductService::make()->baseList(null, 'is_new'));
    }

    /**
     * 推荐商品
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Pagination\Paginator|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|void
     */
    public function recommendedProducts()
    {
        return responses(ProductService::make()->baseList(null, 'is_recommend'));
    }

    /**
     * 助农商品
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Pagination\Paginator|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|void
     */
    public function farmersProducts()
    {
        return responses(ProductService::make()->baseList(null, 'is_farmers'));
    }

    /**
     * 特产商品
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Pagination\Paginator|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response|void
     */
    public function specialtyProducts()
    {
        return responses(ProductService::make()->baseList(null, 'is_specialty'));
    }

    /**
     * 商品类别.
     * @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function productCategory()
    {
        $p = ProductCategory::query()
            ->where('status', 1)
            ->orderByDesc('sort')
            ->get()
            ->toArray();
        return responses((new ProductCategory())->toTree($p));
    }

    /**
     * 商品列表.
     * @return null|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Pagination\Paginator|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function productsList()
    {
        $response = self::validate([
            'category_id' => 'required|integer',
        ]);
        return responses(ProductService::make()->baseList($response->get('category_id')));
    }

    /**
     * 商品详情 登录用户留下足迹.
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function productDetails()
    {
        $response = self::validate([
            'product_id' => 'required|integer',
        ]);
        $product = Product::with(['reviews' => function ($query) {
            /* @var Builder $query */
            $query->with('user')->where('status', 1)->latest()->limit(20);
        }])->find($response->get('product_id'));
        if (blank($product)) {
            return responses([], 7);
        }
        if (self::UserId()) {
            $user = User::query()->find(self::UserId());
            ProductService::make()->footprint($user, $response->get('product_id'));
            return responses($product);
        }
        return responses($product);
    }

    public function searchProduct()
    {
        $request = self::validate([
            'search' => 'required|string',
        ]);
        $res = Product::query()
            ->select(['name', 'id', 'subtitle', 'main_image', 'sold_num', 'price', 'stock'])
            ->where('status', '<>', 0)
            ->where('name', 'like', '%' . $request['search'] . '%')
            ->get();
        return responses($res);
    }

    public function beanPackage()
    {
        $res = BeanPackages::query()
            ->select(['id', 'name', 'price', 'stock', 'image', 'total_amount'])
            ->simplePaginate(request('perPage', 10));
        return responses($res);
    }

    public function beanPackageDetails()
    {
        $request = self::validate([
            'id' => 'required|integer',
        ]);
        $res = BeanPackages::query()
             ->find($request->get('id'));
        return responses($res);
    }
}
